package client.GUI.dashboard;

import client.GUI.GUIClient;
import client.backend.price.Price;
import client.backend.price.PriceController;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.lang.Runnable;

/**
 * Creates a panel that displays the current rate for
 * all currency pairs
 */
public class PricesPanel extends JPanel implements ActionListener, Runnable {

	private String currencyPairs[];
	private static final long serialVersionUID = 1L;
	private JButton prices[];  //  @jve:decl-index=0:visual-constraint="10,84"
	private JLabel pricesLabel[];
	private static PricesPanel instance = null;
	boolean stop = true;
	
	/**
	 * This is the default constructor
	 */
	private PricesPanel() {
		super();
		
		currencyPairs = PriceController.getInstance().getCurrPairs();
		prices = new JButton[currencyPairs.length];
		pricesLabel = new JLabel[currencyPairs.length];
		initialize();
	}
	
	/**
	 * Checks to see if a PricesPanel is already running,
	 * and works with that one. Creates a new one if
	 * there one doesn't already exist
	 * 
	 * @return instance - the PricesPanel that already exists or
	 * 					  null if one doesn't already exist
	 */
	public static PricesPanel getInstance() {
		if (instance == null) {
			instance = new PricesPanel();
		}
		return instance;
	}

	/**
	 * This method initializes this
	 */
	private void initialize() {
		this.setLayout(new GridLayout((currencyPairs.length * 2)/5,5));
		
		initializeButtons();
		
		this.setBackground(Color.black);
		
		
		for (int y = 0; y < (currencyPairs.length)/5; y++) {
			
			for (int x = 0; x < 5; x++) {
				this.add(pricesLabel[5 * y + x]);
			}
			for (int x = 0; x < 5; x++) {
				this.add(prices[5 * y + x]);
				prices[5 * y + x].addActionListener(this);
			}
		}
		
	}
	
	/**
	 * Initializes the buttons
	 */
	private void initializeButtons() {
		for (int x = 0; x < currencyPairs.length; x++)
			prices[x] = new JButton();
		for (int x = 0; x < currencyPairs.length; x++) {
			String cp[] = Price.seperateCurrencies(currencyPairs[x].toString());
			pricesLabel[x] = new JLabel(cp[0] + " - " + cp[1]);
			pricesLabel[x].setForeground(Color.white);
			pricesLabel[x].setHorizontalAlignment(SwingConstants.CENTER);
		}
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return prices - An array of buttons that display
	 * 					the current rate between the
	 * 					respective currencies, and that
	 * 					when clicked, allow the user
	 * 					to create a new order with those currencies
	 */
	public JButton getPricesButton(int x) {
		return prices[x];
	}
	
	/**
	 * Set the price label on the button at index 'x'
	 * 
	 * @param x - The index of the button that is to display the amount in price
	 * @param price - The label that the button at index x will display
	 */
	public void setPrices(int x, float price) {
		prices[x].setText(price + "");
	}
	
	/**
	 * Set the price label on the button at index 'x'
	 * 
	 * @param x - The index of the button that is to display the amount in price
	 * @param price - The label that the button at index x will display
	 */
	public void setPrices(int x, String price) {
		prices[x].setText(price);
	}

	/**
	 * Checks to see if an action is performed and
	 * follows appropriate procedure
	 * 
	 * @param e - The action performed
	 */
	public void actionPerformed(ActionEvent e) {
		for (int x = 0; x < 15; x++) {
				if (e.getSource() == prices[x]) {
					GUIClient.getInstance().newOrder(currencyPairs[x]);
					stop = true;
				}
			}
		
	}
	
	/**
	 * Sets stop
	 * 
	 * @param st - Boolean, stop
	 */
	public void setStop(boolean st) {
		stop = st;
	}

	/**
	 * Updates prices when necessary
	 */
	public void run() {
		while (!stop) {
			if (System.currentTimeMillis() % 5000 == 0) {
				updatePrices();
			}
		}
		System.out.println("ending run");
	}
	
	/**
	 * Updates the prices and the labels displayed on the price buttons
	 */
	public void updatePrices() {
		for (int x = 0; x < currencyPairs.length; x++) {
			String curr1 = pricesLabel[x].getText().substring(0,3);
			String curr2 = pricesLabel[x].getText().substring(6,9);
			prices[x].setText(PriceController.getInstance().getPrice(curr1, curr2).toString());
		}
	}
}
